package com.fish.web.config;

import com.fish.web.interceptor.JwtInterceptor;
import com.fish.web.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    @Autowired
    private JwtInterceptor jwtInterceptor;
    
    @Autowired
    private PermissionInterceptor permissionInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // JWT拦截器 - 用于设置用户上下文（优先级最高，最先执行）
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**")
                .order(1);
        
        // 权限拦截器 - 用于权限校验
        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns(
                        "/api/user/register",
                        "/api/user/login",
                        "/api/user/health",
                        "/api/user/info/**",
                        "/api/user/*",
                        "/api/user/username/**",
                        "/api/user/avatar/**"
                )
                .order(2);
    }
}